package com.fencing_squad.homo.list;

import com.bumptech.glide.Glide;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.util.ImageLoader;
import de.hdodenhof.circleimageview.CircleImageView;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class PostProvider extends BaseItemProvider {
    private List<Post> list;
    private Context context;
    private static final int BEFORE_COUNT = 1;
    private static int page = 0;
    private PageSlider pageSlider;
    private ScheduledExecutorService scheduledExecutorService;
    private Component component;


    public PostProvider(Context context) {
        this.list = new ArrayList<>();
        this.context = context;

        component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ad, null, false);
        pageSlider = (PageSlider) component.findComponentById(ResourceTable.Id_ads);
        AdProvider adProvider = new AdProvider(getAds());
        pageSlider.setProvider(adProvider);
        pageSlider.setCircularModeEnabled(true);

        // 初始化线程池
        this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        // 设置线程池
        this.scheduledExecutorService.scheduleWithFixedDelay(() -> {
            this.context.getUITaskDispatcher().asyncDispatch(() -> {
                if (this.pageSlider != null && this.pageSlider.isComponentDisplayed()) {
                    page = (page + 1) % 3;
                    this.pageSlider.setCurrentPage(page);
                }
            });
        }, 3000, 3000, TimeUnit.MILLISECONDS);
    }

    public class Holder {
        Text nickname;
        Text content;
        Text likes;
        Text comment;
        CircleImageView avatar;

        public Holder(Component component) {
            nickname = (Text) component.findComponentById(ResourceTable.Id_postNickname);
            content = (Text) component.findComponentById(ResourceTable.Id_postContent);
            comment = (Text) component.findComponentById(ResourceTable.Id_postComment);
            likes = (Text) component.findComponentById(ResourceTable.Id_postLikes);
            avatar = (CircleImageView) component.findComponentById(ResourceTable.Id_postIcon);
        }
    }

    public void append(Post post) {
        if(list != null) {
            list.add(post);
            notifyDataChanged();
        }
    }

    public void append(List<Post> post) {
        if(list != null) {
            list.addAll(post);
            notifyDataChanged();
        }
    }

    public void prepend(List<Post> posts) {
        if (list != null) {
            list.addAll(0, posts);
            notifyDataChanged();
        }
    }

    public int getSize() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public void clear() {
        if (list != null) {
            list.clear();
            notifyDataChanged();
        }
    }

    public List<Post> getList() {
        return list;
    }

    @Override
    public int getCount() {
        return list == null ? BEFORE_COUNT : list.size() + BEFORE_COUNT;
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position >= BEFORE_COUNT && position < list.size() + BEFORE_COUNT){
            return list.get(position - BEFORE_COUNT);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        if (position == 0) {
            return component;
        }
        else if (list != null && position >= BEFORE_COUNT && position < list.size() + BEFORE_COUNT) {
            final Component cpt;
            Post item = list.get(position - BEFORE_COUNT);
            Holder holder;
            if (convertComponent == null || convertComponent.getTag() == null || !(convertComponent.getTag() instanceof Holder)) {
                cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_post, null, false);
                holder = new Holder(cpt);
                cpt.setTag(holder);
            } else {
                cpt = convertComponent;
                holder = (Holder)cpt.getTag();
            }
            holder.nickname.setText(item.getNickname());
            holder.likes.setText(item.getLikes());
            holder.comment.setText(item.getComment());
            holder.content.setText(item.getContent());
            Glide.with(cpt.getContext()).load(item.getAvatar()).listener(ImageLoader.fail(holder.avatar, ResourceTable.Media_icon)).into(holder.avatar);
            return cpt;
        }
        return null;
    }

    private List<Component> getAds() {
        List<Component> ads = new ArrayList<>();
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ad_item, null, false);
        Image image = (Image) component.findComponentById(ResourceTable.Id_adImage);
        image.setPixelMap(ResourceTable.Media_ad0);
        ads.add(component);

        component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ad_item, null, false);
        image = (Image) component.findComponentById(ResourceTable.Id_adImage);
        image.setPixelMap(ResourceTable.Media_ad1);
        ads.add(component);

        component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ad_item, null, false);
        image = (Image) component.findComponentById(ResourceTable.Id_adImage);
        image.setPixelMap(ResourceTable.Media_ad2);
        ads.add(component);

        return ads;
    }
}
